import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

def histogram_demo(image):
    plt.hist(image.ravel(), 256, [0, 256])#ravel函数功能是将多维数组降为一维数组
    plt.title(u"直方图")

    plt.xlabel(u"像素值")
    plt.ylabel(u"数量",rotation=0)
    #plt.legend((u’头等舱’, u’2等舱’, u’3等舱’,), loc=‘best’, prop = font)
    plt.show()



image = cv.imread('2.png', 1)
cv.imshow('souce image', image)
histogram_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
